﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using WAD.Model.WeiXin;

namespace WAD.DAL
{
    public class DAOWxJsApi : DAO
    {
        public static int Update(JsApiAccessToken token)
        {
            var db = GetDatabase();
            const string sql = "update wx_jsapi_token set access_token=@access_token,token_expire=@token_expire,jsapi_ticket=@jsapi_ticket,ticket_expire=@ticket_expire where appid=@appid";
            var cmd = db.GetSqlStringCommand(sql);
            if (Count() <= 0) return Insert(token);
            db.AddInParameter(cmd, "@access_token", DbType.String, token.Access_Token);
            db.AddInParameter(cmd, "@token_expire", DbType.DateTime, token.Token_Expire);
            db.AddInParameter(cmd, "@jsapi_ticket", DbType.String, token.JsApi_Ticket);
            db.AddInParameter(cmd, "@ticket_expire", DbType.DateTime, token.Ticket_Expire);
            db.AddInParameter(cmd, "@appid", DbType.String, token.AppId);
            return db.ExecuteNonQuery(cmd);
        }

        private static int Count()
        {
            var db = GetDatabase();
            const string sqlcount = "select count(*) from wx_jsapi_token";
            var cmdcount = db.GetSqlStringCommand(sqlcount);
            var count = db.ExecuteScalar(cmdcount) ?? 0;
            return (int) count;
        }

        private static int Insert(JsApiAccessToken token)
        {
            const string sql = "insert into wx_jsapi_token (appid,access_token,token_expire,jsapi_ticket,ticket_expire)values(@appid,@access_token,@token_expire,@jsapi_ticket,@ticket_expire)";
            var db = GetDatabase();
            var cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@access_token", DbType.String, token.Access_Token);
            db.AddInParameter(cmd, "@token_expire", DbType.DateTime, token.Token_Expire);
            db.AddInParameter(cmd, "@jsapi_ticket", DbType.String, token.JsApi_Ticket);
            db.AddInParameter(cmd, "@ticket_expire", DbType.DateTime, token.Ticket_Expire);
            db.AddInParameter(cmd, "@appid", DbType.String, token.AppId);
            return db.ExecuteNonQuery(cmd);
        }
        public static JsApiAccessToken Get()
        {
            var token = new JsApiAccessToken();
            const string sql = "select top 1 appid,access_token,token_expire,jsapi_ticket,ticket_expire from wx_jsapi_token";
            var db = GetDatabase();
            var cmd = db.GetSqlStringCommand(sql);
            using (var reader = db.ExecuteReader(cmd))
            {
                if (!reader.Read()) return token;
                token.AppId = reader.GetString(0);
                token.Access_Token = reader.GetString(1);
                token.Token_Expire = reader.GetDateTime(2);
                token.JsApi_Ticket = reader.GetString(3);
                token.Ticket_Expire = reader.GetDateTime(4);
            }
            
            return token;
        }
    }
}
